package com.purplesoft.zettl.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.purplesoft.zettl.R;
import com.purplesoft.zettl.DatabaseTables.TblCart;
import com.purplesoft.zettl.DatabaseTables.TblCategories;
import com.purplesoft.zettl.DatabaseTables.TblItems;
import com.purplesoft.zettl.DatabaseTables.TblLists;
import com.purplesoft.zettl.DatabaseTables.TblManufacturer;
import com.purplesoft.zettl.DatabaseTables.TblMeasures;
import com.purplesoft.zettl.DatabaseTables.TblPurchase;
import com.purplesoft.zettl.DatabaseViews.VwCartItems;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;
import com.purplesoft.zettl.Provider.DatabaseContentProvider;
import com.purplesoft.zettl.Utilities.CurrentDateTime;
import com.purplesoft.zettl.Utilities.Item;
import com.purplesoft.zettl.Utilities.SharedPrefs;

public class DatabaseExecuter {
	
	private Cursor csVwPurchaseItems, csVwCartItems, csTblItems, csTblCart, csTblPurchase, csTblLists, csTblManufacturer, csTblMeasures, csTblCategories;
	private Context context;
	private Item item;
	private SharedPrefs sharedPrefs;
	
	private final int DEFAULT_VALUE = -99;
	
	public DatabaseExecuter(Context context) {
		this.context = context;
		sharedPrefs = new SharedPrefs(context);
	}
	
	public Item setItemAllAttributesFromCsVwPurchaseItems (String itemName) {
		String vwPurchaseItemsWhereClause = VwPurchaseItems.Columns.Item + " = ? AND " + VwPurchaseItems.Columns.ListID + " = ?";
		String[] vwPurchaseItemsWhereClauseArgs = { itemName, String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
		csVwPurchaseItems = context.getContentResolver().query(DatabaseContentProvider.VWPurchaseItems, null, vwPurchaseItemsWhereClause, vwPurchaseItemsWhereClauseArgs, null);
		csVwPurchaseItems.moveToFirst();
		item = new Item(itemName);
		item.setItemAllAttributes(csVwPurchaseItems);
		csVwPurchaseItems.close();
		return item;
	}
	public Item setItemAllAttributesFromCsVwCartItems (String itemName) {
		String vwCartItemsWhereClause = VwCartItems.Columns.Item + " = ? AND " + VwCartItems.Columns.ListID + " = ?";
		String[] vwCartItemsWhereClauseArgs = { itemName, String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
		csVwCartItems = context.getContentResolver().query(DatabaseContentProvider.VWCartItems, null, vwCartItemsWhereClause, vwCartItemsWhereClauseArgs, null);
		csVwCartItems.moveToFirst();
		item = new Item(itemName);
		item.setItemAllAttributes(csVwCartItems);
		csVwCartItems.close();
		return item;
	}

	public Cursor queryFromVwPurchaseItems(String column, int value) {
		String vwPurchaseItemsWhereClause = column + " = ?";
		String[] vwPurchaseItemsWhereClauseArgs = {  String.valueOf(value) };
		return csVwPurchaseItems = context.getContentResolver().query(DatabaseContentProvider.VWPurchaseItems, null, vwPurchaseItemsWhereClause, vwPurchaseItemsWhereClauseArgs, null);
	}

	public Cursor queryFromVwPurchaseItems(String column, String value) {
		String vwPurchaseItemsWhereClause = column + " = ?";
		String[] vwPurchaseItemsWhereClauseArgs = {  value };
		csVwPurchaseItems = context.getContentResolver().query(DatabaseContentProvider.VWPurchaseItems, null, vwPurchaseItemsWhereClause, vwPurchaseItemsWhereClauseArgs, null);
		csVwPurchaseItems.moveToFirst();
		return csVwPurchaseItems;
	}

	public Cursor queryFromVwCartItems(String column, int value) {
		String vwCartItemsWhereClause = column + " = ?";
		String[] vwCartItemsWhereClauseArgs = {  String.valueOf(value) };
		return csVwCartItems = context.getContentResolver().query(DatabaseContentProvider.VWCartItems, null, vwCartItemsWhereClause, vwCartItemsWhereClauseArgs, null);
	}

	public Cursor queryFromTblLists(String column, String value, String operation) {
		String tblListsWhereClause = column + operation + "?";
		String[] tblListsWhereClauseArgs = { value };
		csTblLists = context.getContentResolver().query(DatabaseContentProvider.TBLLists, null, tblListsWhereClause, tblListsWhereClauseArgs, null);
		csTblLists.moveToFirst();
		return csTblLists;
	}

	public Cursor queryFromTblItems(String column, String value, String operation) {
		String tblItemsWhereClause = column + operation + "?";
		String[] tblItemssWhereClauseArgs = { value };
		return csTblItems = context.getContentResolver().query(DatabaseContentProvider.TBLItems, null, tblItemsWhereClause, tblItemssWhereClauseArgs, null);
	}

	public Cursor queryFromTblLists(String column, int value, String operation) {
		String tblListsWhereClause = column + operation + "?";
		String[] tblListsWhereClauseArgs = { String.valueOf(value) };
		return csTblLists = context.getContentResolver().query(DatabaseContentProvider.TBLLists, null, tblListsWhereClause, tblListsWhereClauseArgs, null);
	}

	public Cursor queryFromTblManufacturer(String column, int value, String operation) {
		String tblManufacturerWhereClause = column + operation + "?";
		String[] tblManufacturerWhereClauseArgs = { String.valueOf(value) };
		csTblManufacturer = context.getContentResolver().query(DatabaseContentProvider.TBLManufacturer, null, tblManufacturerWhereClause, tblManufacturerWhereClauseArgs, null);
		csTblManufacturer.moveToFirst();
		return  csTblManufacturer;
	}

	public Cursor queryFromTblManufacturer(String column, String value, String operation) {
		String tblManufacturerWhereClause = column + operation + "?";
		String[] tblManufacturerWhereClauseArgs = { value };
		csTblManufacturer = context.getContentResolver().query(DatabaseContentProvider.TBLManufacturer, null, tblManufacturerWhereClause, tblManufacturerWhereClauseArgs, null);
		csTblManufacturer.moveToFirst();
		return  csTblManufacturer;
	}

	public Cursor queryFromTblMeasures(String column, int value, String operation) {
		String tblMeasuresWhereClause = column + operation + "?";
		String[] tblMeasuresWhereClauseArgs = { String.valueOf(value) };
		csTblMeasures = context.getContentResolver().query(DatabaseContentProvider.TBLMeasures, null, tblMeasuresWhereClause, tblMeasuresWhereClauseArgs, null);
		csTblMeasures.moveToFirst();
		return  csTblMeasures;
	}

	public Cursor queryFromTblMeasures(String column, String value, String operation) {
		String tblMeasuresWhereClause = column + operation + "?";
		String[] tblMeasuresWhereClauseArgs = { value };
		csTblMeasures = context.getContentResolver().query(DatabaseContentProvider.TBLMeasures, null, tblMeasuresWhereClause, tblMeasuresWhereClauseArgs, null);
		csTblMeasures.moveToFirst();
		return  csTblMeasures;
	}

	public Cursor queryFromTblCategories(String column, int value, String operation) {
		String tblCategoriesWhereClause = column + operation + "?";
		String[] tblCategoriesWhereClauseArgs = { String.valueOf(value) };
		csTblCategories = context.getContentResolver().query(DatabaseContentProvider.TBLCategories, null, tblCategoriesWhereClause, tblCategoriesWhereClauseArgs, null);
		csTblCategories.moveToFirst();
		return  csTblCategories;
	}

	public Cursor queryFromTblCategories(String column, String value, String operation) {
		String tblCategoriesWhereClause = column + operation + "?";
		String[] tblCategoriesWhereClauseArgs = { value };
		csTblCategories = context.getContentResolver().query(DatabaseContentProvider.TBLCategories, null, tblCategoriesWhereClause, tblCategoriesWhereClauseArgs, null);
		csTblCategories.moveToFirst();
		return  csTblCategories;
	}

	public Cursor queryFromTblCart(String column, int value) {
		String tblCartWhereClause = column + " = ?";
		String[] tblcartsWhereClauseArgs = { String.valueOf(value) };
		return csTblCart = context.getContentResolver().query(DatabaseContentProvider.TBLCart, null, tblCartWhereClause, tblcartsWhereClauseArgs, null);
	}

	public void insertIntoTblPurchase(Item item) {
		
		ContentValues contentValues = new ContentValues();
		String currentDateTime = new CurrentDateTime().getNow();
		String tblPurchaseWhereClause = TblPurchase.Columns.ItemID + " = ? AND " + TblPurchase.Columns.ListID + " = ?";
		String[] tblPurchaseWhereClauseArgs = { String.valueOf(item.getID()), String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
		csTblPurchase = context.getContentResolver().query(DatabaseContentProvider.TBLPurchase, null, tblPurchaseWhereClause, tblPurchaseWhereClauseArgs, null);
		csTblPurchase.moveToFirst();
		if (csTblPurchase.getCount() == 1) {
			String tblCartWhereClause = TblCart.Columns.ItemID + " = ? AND " + TblCart.Columns.ListID + " = ?";
			String[] tblCartWhereClauseArgs = { String.valueOf(item.getID()), String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
			csTblCart = context.getContentResolver().query(DatabaseContentProvider.TBLCart, null, tblCartWhereClause, tblCartWhereClauseArgs, null);
			csTblCart.moveToFirst();
			float amountTblPurchase = csTblPurchase.getInt(csTblPurchase.getColumnIndex(TblPurchase.Columns.Amount));
			float amountTblCart = csTblCart.getInt(csTblCart.getColumnIndex(TblCart.Columns.Amount));
			csTblCart.close();
			amountTblPurchase = amountTblPurchase + amountTblCart;
			contentValues.put(TblPurchase.Columns.Amount, amountTblPurchase);
			contentValues.put(TblPurchase.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLPurchase, contentValues, tblPurchaseWhereClause, tblPurchaseWhereClauseArgs);
		} else {
			contentValues.put(TblPurchase.Columns.ItemID, item.getID());
			contentValues.put(TblPurchase.Columns.Amount, item.getAmount());
			contentValues.put(TblPurchase.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE));
			contentValues.put(TblPurchase.Columns.Created, currentDateTime);
			contentValues.put(TblPurchase.Columns.Modified, currentDateTime);
			context.getContentResolver().insert(DatabaseContentProvider.TBLPurchase, contentValues);
		}
		csTblPurchase.close();
	}

	public boolean insertIntoTblLists(String listName) {
		String tblListsWhereClause = TblLists.Columns.ListName + " = ?";
		String[] tblListsWhereClauseArgs = {  listName };
		csTblLists = context.getContentResolver().query(DatabaseContentProvider.TBLLists, null, tblListsWhereClause, tblListsWhereClauseArgs, null);
		if (csTblLists.getCount() != 0) {
			csTblLists.close();
			return false;
		} else {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblLists.Columns.ListName, listName);
			contentValues.put(TblLists.Columns.Created, currentDateTime);
			contentValues.put(TblLists.Columns.Modified, currentDateTime);
			context.getContentResolver().insert(DatabaseContentProvider.TBLLists, contentValues);
			return true;
		}
	}

	public boolean insertIntoTblManufacturer(String manufacturer) {
		String tblManufacturerWhereClause = TblManufacturer.Columns.Manufacturer + " = ?";
		String[] tblManufacturerWhereClauseArgs = {  manufacturer };
		csTblManufacturer = context.getContentResolver().query(DatabaseContentProvider.TBLManufacturer, null, tblManufacturerWhereClause, tblManufacturerWhereClauseArgs, null);
		if (csTblManufacturer.getCount() != 0) {
			csTblManufacturer.close();
			return false;
		} else {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblManufacturer.Columns.Manufacturer, manufacturer);
			contentValues.put(TblManufacturer.Columns.Created, currentDateTime);
			contentValues.put(TblManufacturer.Columns.Modified, currentDateTime);
			context.getContentResolver().insert(DatabaseContentProvider.TBLManufacturer, contentValues);
			return true;
		}
	}

	public boolean insertIntoTblMeasures(String measure, String measureShort, String measureMin, String measureMax, String measureIncreaseBy, String measurePricePer) {
		String tblMeasuresWhereClause = TblMeasures.Columns.Measure + " = ?";
		String[] tblMeasuresWhereClauseArgs = {  measure };
		csTblMeasures = context.getContentResolver().query(DatabaseContentProvider.TBLMeasures, null, tblMeasuresWhereClause, tblMeasuresWhereClauseArgs, null);
		if (csTblMeasures.getCount() != 0) {
			csTblMeasures.close();
			return false;
		} else {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblMeasures.Columns.Measure, measure);
			contentValues.put(TblMeasures.Columns.MeasureShort, measureShort);
			contentValues.put(TblMeasures.Columns.Min, measureMin);
			contentValues.put(TblMeasures.Columns.Max, measureMax);
			contentValues.put(TblMeasures.Columns.IncreaseBy, measureIncreaseBy);
			contentValues.put(TblMeasures.Columns.PricePer, measurePricePer);
			contentValues.put(TblMeasures.Columns.Created, currentDateTime);
			contentValues.put(TblMeasures.Columns.Modified, currentDateTime);
			context.getContentResolver().insert(DatabaseContentProvider.TBLMeasures, contentValues);
			return true;
		}
	}

	public boolean insertIntoTblCategories(String category, String categoryColor) {
		String tblCategoriesWhereClause = TblCategories.Columns.CategoryName + " = ?";
		String[] tblCategoriesWhereClauseArgs = {  category };
		csTblCategories = context.getContentResolver().query(DatabaseContentProvider.TBLCategories, null, tblCategoriesWhereClause, tblCategoriesWhereClauseArgs, null);
		if (csTblCategories.getCount() != 0) {
			csTblCategories.close();
			return false;
		} else {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblCategories.Columns.CategoryName, category);
			contentValues.put(TblCategories.Columns.CategoryColor, categoryColor);
			contentValues.put(TblCategories.Columns.Created, currentDateTime);
			contentValues.put(TblCategories.Columns.Modified, currentDateTime);
			context.getContentResolver().insert(DatabaseContentProvider.TBLCategories, contentValues);
			return true;
		}
	}

	public void insertIntoTblCart(Item item) {
		
		ContentValues contentValues = new ContentValues();
		String currentDateTime = new CurrentDateTime().getNow();
		String tblCartWhereClause = TblCart.Columns.ItemID + " = ? AND " + TblCart.Columns.ListID + " = ?";
		String[] tblCartWhereClauseArgs = { String.valueOf(item.getID()), String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
		csTblCart = context.getContentResolver().query(DatabaseContentProvider.TBLCart, null, tblCartWhereClause, tblCartWhereClauseArgs, null);
		csTblCart.moveToFirst();
		if (csTblCart.getCount() == 1) {
			String tblPurchaseWhereClause = TblPurchase.Columns.ItemID + " = ? AND " + TblPurchase.Columns.ListID + " = ?";
			String[] tblPurchaseWhereClauseArgs = { String.valueOf(item.getID()), String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
			csTblPurchase = context.getContentResolver().query(DatabaseContentProvider.TBLPurchase, null, tblPurchaseWhereClause, tblPurchaseWhereClauseArgs, null);
			csTblPurchase.moveToFirst();
			float amountTblCart = csTblCart.getInt(csTblCart.getColumnIndex(TblCart.Columns.Amount));
			float amountTblPurchase = csTblPurchase.getInt(csTblPurchase.getColumnIndex(TblPurchase.Columns.Amount));
			csTblPurchase.close();
			amountTblCart = amountTblCart + amountTblPurchase;
			contentValues.put(TblCart.Columns.Amount, amountTblCart);
			contentValues.put(TblCart.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLCart, contentValues, tblCartWhereClause, tblCartWhereClauseArgs);
		} else {
			contentValues.put(TblCart.Columns.ItemID, item.getID());
			contentValues.put(TblCart.Columns.Amount, item.getAmount());
			contentValues.put(TblCart.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE));
			contentValues.put(TblCart.Columns.Created, currentDateTime);
			contentValues.put(TblCart.Columns.Modified, currentDateTime);
			context.getContentResolver().insert(DatabaseContentProvider.TBLCart, contentValues);
		}
		csTblCart.close();
	}

	public boolean updateTblList(String listName, String newListName) {
		String tblListsWhereClause = TblLists.Columns.ListName + " = ?";
		String[] tblListsWhereClauseArgs = {  listName };
		String[] tblListsWhereClauseArgsOld = {  newListName };
		csTblLists = context.getContentResolver().query(DatabaseContentProvider.TBLLists, null, tblListsWhereClause, tblListsWhereClauseArgsOld, null);
		if (csTblLists.getCount() != 0) {
			csTblLists.close();
			return false;
		} else {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblLists.Columns.ListName, newListName);
			contentValues.put(TblLists.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLLists, contentValues, tblListsWhereClause, tblListsWhereClauseArgs);
			return true;
		}
	}

	public boolean updateTblManufacturer(String manufacturer, String newManufacturer) {
		String tblManufacturerWhereClause = TblManufacturer.Columns.Manufacturer + " = ?";
		String[] tblManufacturerWhereClauseArgs = {  manufacturer };
		String[] tblManufacturerWhereClauseArgsOld = {  newManufacturer };
		csTblManufacturer = context.getContentResolver().query(DatabaseContentProvider.TBLManufacturer, null, tblManufacturerWhereClause, tblManufacturerWhereClauseArgsOld, null);
		if (csTblManufacturer.getCount() != 0) {
			csTblManufacturer.close();
			return false;
		} else {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblManufacturer.Columns.Manufacturer, newManufacturer);
			contentValues.put(TblManufacturer.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLManufacturer, contentValues, tblManufacturerWhereClause, tblManufacturerWhereClauseArgs);
			return true;
		}
	}

	public boolean updateTblMeasures(String measure, String newMeasure, String measureShort, String measureMin, String measureMax, String measureIncreaseBy, String measurePricePer) {
		String tblMeasuresWhereClause = TblMeasures.Columns.Measure + " = ?";
		String[] tblMeasuresWhereClauseArgs = {  measure };
		String[] tblMeasuresWhereClauseArgsOld = {  newMeasure };
		String currentDateTime = new CurrentDateTime().getNow();
		ContentValues contentValues = new ContentValues();
		contentValues.put(TblMeasures.Columns.Measure, newMeasure);
		contentValues.put(TblMeasures.Columns.MeasureShort, measureShort);
		contentValues.put(TblMeasures.Columns.Min, measureMin);
		contentValues.put(TblMeasures.Columns.Max, measureMax);
		contentValues.put(TblMeasures.Columns.IncreaseBy, measureIncreaseBy);
		contentValues.put(TblMeasures.Columns.PricePer, measurePricePer);
		contentValues.put(TblMeasures.Columns.Modified, currentDateTime);
		if (measure.equals(newMeasure)) {
			context.getContentResolver().update(DatabaseContentProvider.TBLMeasures, contentValues, tblMeasuresWhereClause, tblMeasuresWhereClauseArgs);
			return true;
		} else {
			csTblMeasures = context.getContentResolver().query(DatabaseContentProvider.TBLMeasures, null, tblMeasuresWhereClause, tblMeasuresWhereClauseArgsOld, null);
			if (csTblMeasures.getCount() != 0) {
				csTblMeasures.close();
				return false;
			} else {
				context.getContentResolver().update(DatabaseContentProvider.TBLMeasures, contentValues, tblMeasuresWhereClause, tblMeasuresWhereClauseArgs);
				return true;
			}
		}
	}

	public boolean updateTblCategories(String category, String newCategory, String newCategoryColor) {
		String tblCategoriesWhereClause = TblCategories.Columns.CategoryName + " = ?";
		String[] tblCategoriesWhereClauseArgs = {  category };
		String[] tblCategoriesWhereClauseArgsOld = {  newCategory };
		if (category.equals(newCategory)) {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(TblCategories.Columns.CategoryName, newCategory);
			contentValues.put(TblCategories.Columns.CategoryColor, newCategoryColor);
			contentValues.put(TblCategories.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLCategories, contentValues, tblCategoriesWhereClause, tblCategoriesWhereClauseArgs);
			return true;
		} else {
			csTblCategories = context.getContentResolver().query(DatabaseContentProvider.TBLCategories, null, tblCategoriesWhereClause, tblCategoriesWhereClauseArgsOld, null);
			if (csTblCategories.getCount() != 0) {
				csTblCategories.close();
				return false;
			} else {
				String currentDateTime = new CurrentDateTime().getNow();
				ContentValues contentValues = new ContentValues();
				contentValues.put(TblCategories.Columns.CategoryName, newCategory);
				contentValues.put(TblCategories.Columns.CategoryColor, newCategoryColor);
				contentValues.put(TblCategories.Columns.Modified, currentDateTime);
				context.getContentResolver().update(DatabaseContentProvider.TBLCategories, contentValues, tblCategoriesWhereClause, tblCategoriesWhereClauseArgs);
				return true;
			}
		}
	}

	public boolean updateTblItems(String column, int oldValue, int newValue) {
		String tblItemsWhereClause = column + " = ?";
		String[] tblItemsWhereClauseArgs = {  String.valueOf(oldValue) };
		csTblItems = context.getContentResolver().query(DatabaseContentProvider.TBLItems, null, tblItemsWhereClause, tblItemsWhereClauseArgs, null);
		if (csTblItems.getCount() != 0) {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(column, newValue);
			contentValues.put(TblItems.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLItems, contentValues, tblItemsWhereClause, tblItemsWhereClauseArgs);
			return true;
		} else {
			csTblItems.close();
			return false;
		}
	}

	public boolean updateTblItems(String searchColumn, String updateColumn, String searchValue, String updateValue) {
		String tblItemsWhereClause = searchColumn + " = ?";
		String[] tblItemsWhereClauseArgs = {  searchValue };
		csTblItems = context.getContentResolver().query(DatabaseContentProvider.TBLItems, null, tblItemsWhereClause, tblItemsWhereClauseArgs, null);
		if (csTblItems.getCount() != 0) {
			String currentDateTime = new CurrentDateTime().getNow();
			ContentValues contentValues = new ContentValues();
			contentValues.put(updateColumn, updateValue);
			contentValues.put(TblItems.Columns.Modified, currentDateTime);
			context.getContentResolver().update(DatabaseContentProvider.TBLItems, contentValues, tblItemsWhereClause, tblItemsWhereClauseArgs);
			return true;
		} else {
			csTblItems.close();
			return false;
		}
	}

	public void deleteFromTblItems(Item item) {
		String tblCartWhereClause = TblItems.Columns._ID + " = ? ";
		String[] tblCartWhereClauseArgs = { String.valueOf(item.getID()) };
		context.getContentResolver().delete(DatabaseContentProvider.TBLItems, tblCartWhereClause, tblCartWhereClauseArgs);
	}

	public void deleteFromTblCart(Item item) {
		String tblCartWhereClause = TblCart.Columns.ItemID + " = ? AND " + TblCart.Columns.ListID + " = ?";
		String[] tblCartWhereClauseArgs = { String.valueOf(item.getID()), String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
		context.getContentResolver().delete(DatabaseContentProvider.TBLCart, tblCartWhereClause, tblCartWhereClauseArgs);
	}

	public void deleteFromTblCart(String column, int value) {
		String tblCartWhereClause = column + " = ?";
		String[] tblCartWhereClauseArgs = { String.valueOf(value) };
		context.getContentResolver().delete(DatabaseContentProvider.TBLCart, tblCartWhereClause, tblCartWhereClauseArgs);
	}

	public void deleteFromTblPurchase(Item item) {
		String tblPurchaseWhereClause = TblPurchase.Columns.ItemID + " = ? AND " + TblPurchase.Columns.ListID + " = ?";
		String[] tblPurchaseWhereClauseArgs = { String.valueOf(item.getID()), String.valueOf(sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) };
		context.getContentResolver().delete(DatabaseContentProvider.TBLPurchase, tblPurchaseWhereClause, tblPurchaseWhereClauseArgs);
	}

	public void deleteFromTblPurchase(String column, int value) {
		String tblPurchaseWhereClause = column + " = ?";
		String[] tblPurchaseWhereClauseArgs = { String.valueOf(value) };
		context.getContentResolver().delete(DatabaseContentProvider.TBLPurchase, tblPurchaseWhereClause, tblPurchaseWhereClauseArgs);
	}

	public void deleteFromTblLists(String listName) {
		String tblListsWhereClause = TblLists.Columns.ListName + " = ?";
		String[] tblListsWhereClauseArgs = { listName };
		csTblLists = context.getContentResolver().query(DatabaseContentProvider.TBLLists, null, tblListsWhereClause, tblListsWhereClauseArgs, null);
		csTblLists.moveToFirst();
		if (csTblLists.getInt(csTblLists.getColumnIndex(TblLists.Columns._ID)) == sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE)) {
			sharedPrefs.setInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE);
		}
		context.getContentResolver().delete(DatabaseContentProvider.TBLLists, tblListsWhereClause, tblListsWhereClauseArgs);
		csTblLists = context.getContentResolver().query(DatabaseContentProvider.TBLLists, null, null, null, null);
		csTblLists.moveToFirst();
		if (csTblLists.getCount() == 1 && csTblLists.getInt(csTblLists.getColumnIndex(TblLists.Columns._ID)) == DEFAULT_VALUE) {
			sharedPrefs.setInt(context.getResources().getString(R.string.zettl_current_list_id), DEFAULT_VALUE);
		}
	}

	public void deleteFromTblManufacturer(String manufacturer) {
		String tblManufacturerWhereClause = TblManufacturer.Columns.Manufacturer + " = ?";
		String[] tblManufacturerWhereClauseArgs = { manufacturer };
		context.getContentResolver().delete(DatabaseContentProvider.TBLManufacturer, tblManufacturerWhereClause, tblManufacturerWhereClauseArgs);
	}

	public void deleteFromTblMeasures(String measure) {
		String tblMeasuresWhereClause = TblMeasures.Columns.Measure + " = ?";
		String[] tblMeasuresWhereClauseArgs = { measure };
		context.getContentResolver().delete(DatabaseContentProvider.TBLMeasures, tblMeasuresWhereClause, tblMeasuresWhereClauseArgs);
	}

	public void deleteFromTblCategories(String category) {
		String tblCategoriesWhereClause = TblCategories.Columns.CategoryName + " = ?";
		String[] tblCategoriesWhereClauseArgs = { category };
		context.getContentResolver().delete(DatabaseContentProvider.TBLCategories, tblCategoriesWhereClause, tblCategoriesWhereClauseArgs);
	}
}
